/*
 * 创建用户：mleo
 * 项目名称：demo
 * 模块名称：demo
 * 文件名称：Web
 * 修改时间：2017/11/14 13:55:27
 * Copyright (c) 2017.
 */

package com.ichochy.data;

import com.google.gson.Gson;
import com.ichochy.data.entity.*;
import com.ichochy.http.AreaInfo;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Hello world!
 */
public class Web implements Serializable {
    private static final long serialVersionUID = 1L;
    static Gson gson = new Gson();
    static List<AreaInfo> allInfos = new ArrayList<>();
    static String parameters = "{\"ProjectId\":%s,\"_rand\":0.2546624498753626}";
    static String GetProjectInfo = "http://pf.vlinker.com.cn:10009/PF_Project/GetProjectInfo?parameters=";
    static String ListOccupancyRates = "http://pf.vlinker.com.cn:10009/PF_Project/ListOccupancyRates?parameters=";
    static String ListProjectFees = "http://pf.vlinker.com.cn:10009/PF_Project/ListProjectFees?parameters=";
    static String ListImage = "http://pf.vlinker.com.cn:10009/PF_Project/ListImage?parameters=";
    static String ListAreaConfigs = "http://pf.vlinker.com.cn:10009/PF_Project/ListAreaConfigs?parameters=";
    static String ListProduct = "http://pf.vlinker.com.cn:10009/PF_Product/ListProduct?parameters=";
    static String cookie = "DOONCEID=69; DOONCEURL=/PopularFeePC/index; ASP.NET_SessionId=1nfakee5qto0pq2dko4o2j4x; __RequestVerificationToken=kqJQEMaSknQfVfi0oDyuIeFmbcRocAReDeEz-17JbxpsSsa3aEeyKqu3MGy0ds71uFhM6rKo-RqUQFjkptyDQWyVlneafEQ0jXAICxDbc6fK4q4p86x8oMF6bHL17OXRpKOJl0sWw--OIcWtKfrFnQ2; .ASPXAUTH=75C3F684CA0C91746DDE126F23B83D5EAAA2A21F50F77F0C13897E451889BB81046404D562374F3E326DCEAD7B99FEDB22B427CFFCB39C8CF7A9B8886E0E8144C894C640EA6E6C5361E680D0BED37F8380D54F473BE383B078091CF220F42232CC8278CC7AD603FB4B5C0867ABEFB0D51377E9BE35EEF871062D4EBF5FC59E2DEA71B3C35EF91D3AD47B314513E048A9";


    public static void main(String[] args) throws Exception {
        String json = httpGetHtml("https://www.baidu.com");
        System.out.println(json);
    }


//    public static void main(String[] args) throws Exception {
//        for (int i = 2191; i <= 2236; i++) {
//            getAllInfo(String.valueOf(i));
//        }
//    }

    public static void getAllInfo(String projectId) throws Exception {
        getUrlInfo(GetProjectInfo,projectId,ProjectInfo.class);
        getUrlInfo(ListOccupancyRates,projectId,OccupancyRates.class);
        getUrlInfo(ListProjectFees,projectId,ProjectFees.class);
        getUrlInfo(ListImage,projectId,Image.class);
        getUrlInfo(ListAreaConfigs,projectId,AreaConfigs.class);
        getUrlInfo(ListProduct,projectId,Product.class);
    }


    public static void getUrlInfo(String url,String projectId,Class clazz) throws Exception {
        String parameter = String.format(parameters, projectId);
        url = url.concat(URLEncoder.encode(parameter, "UTF-8"));
        String json = httpGetHtml(url);
        System.out.println(json);
        getJsonInfo(json, clazz);
    }


    public static void getJsonInfo(String json, Class clazz) throws Exception {
        Result result = gson.fromJson(json, Result.class);
        Object datas = result.getData();
        if(datas instanceof List){
            for (Object data:(List)datas) {
                printSQL(clazz.getSimpleName(),(Map<String,String>)data);
            }
        }else{
            printSQL(clazz.getSimpleName(),(Map<String,String>)datas);
        }
//
    }


    public static void printSQL(String table,Map<String, String> map) throws Exception {
        StringBuilder sql = new StringBuilder();
        StringBuilder keys = new StringBuilder();
        StringBuilder values = new StringBuilder();
        for ( Map.Entry<String,String> entity:map.entrySet()) {
            keys.append("'").append(entity.getKey()).append("',");
            values.append("'").append(String.valueOf(entity.getValue())).append("',");
        }
        sql.append("insert into ").append(table).append(" (");
        sql.append(keys.substring(0,keys.length()-1));
        sql.append(") values (");
        sql.append(values.substring(0,values.length()-1));
        sql.append(");");
        writeSQL(sql.toString());
    }

    public static void writeSQL(String sql) throws Exception {
        FileWriter file = new FileWriter("txt.sql",true);
        BufferedWriter writer = new BufferedWriter(file);
        writer.write(sql);
        writer.newLine();
        writer.close();
        file.close();
    }


    public static String httpGetHtml(String url) throws Exception {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpHost proxy = new HttpHost("127.0.0.1", 8880);
        RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
        HttpGet http = new HttpGet(url);
        http.setHeader("Cookie", cookie);
        http.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
//		http.setConfig(config);
        CloseableHttpResponse response = client.execute(http);
        HttpEntity entity = response.getEntity();
        String result = EntityUtils.toString(entity, "UTF-8");
        response.close();
        client.close();
        return result;
    }


}
